//
//  SLViewController.m
//  SoccerLottery
//
//  Created by chuanshuangzhang on 15/12/25.
//  Copyright © 2015年 chuanshuangzhang. All rights reserved.
//

#import "SLViewController.h"
#import "SLNavigationViewController.h"

@implementation SLViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    _imageView = [[UIImageView alloc]init];
    _imageView.contentMode = UIViewContentModeScaleToFill;
    _imageView.userInteractionEnabled = YES;
    if(IPHONE4S){
        self.imageView.image = [UIImage imageNamed:@"launch_4s"];
    }else if(IPHONE5) {
        _imageView.image = [UIImage imageNamed:@"launch_5"];
    }else if(IPHONE6) {
        _imageView.image = [UIImage imageNamed:@"launch_6"];
    }else if(IPHONE6S) {
        _imageView.image = [UIImage imageNamed:@"launch_6s"];
    }
    _imageView.frame = [UIScreen mainScreen].bounds;
    [self.view addSubview:_imageView];
}
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2*NSEC_PER_SEC), dispatch_get_main_queue(), ^{
        [[[[UIApplication sharedApplication] delegate] window] addSubview:_imageView];
        NSDictionary *dic = [SLDBManager userAccount];
        if(dic == nil){
            [self enterHome];
        }else {
            [self loginWithDic:dic];
        }
        //获取轮播图片
        [SLNewNetMgr getPicture:^(id result, BOOL status, NSError *error) {
            if(NET_SUCCESS(result, status)){
                [SLCache sharedInstance].feedArray = result[@"result"];
            }
        }];
    });
}
- (void)loginWithDic:(NSDictionary *)dict
{
    if(![VWebService isConnected]){
        [self enterHome];
        return;
    }
    [SLUserNetMgr login:dict[@"account"] password:dict[@"passcode"] block:^(id result, BOOL status, NSError *error) {
        if(NET_SUCCESS(result, status)){
            NSDictionary *dic = result[@"result"];
            [UserModel sharedInstance].userId = CS(dic[@"UserID"]);
            [UserModel sharedInstance].phone = CS(dic[@"Telephone"]);
            [UserModel sharedInstance].password = CS(dic[@"Password"]);
            [UserModel sharedInstance].userName = CS(dic[@"UserName"]);
            NSInteger coins = [CS(dic[@"Points"]) integerValue];
            NSString *localCoins = [[UserModel sharedInstance] getLocalCoins];
            [[UserModel sharedInstance] clearLocalCoins];
            if([localCoins integerValue] > 0){
                [SLNewNetMgr setCoins:[UserModel sharedInstance].userId coins:localCoins recommandId:@"" block:^(id result, BOOL status, NSError *error) {
                }];
            }
            [UserModel sharedInstance].coin = [NSString stringWithFormat:@"%ld",coins + [localCoins integerValue]];
            [SLUserNetMgr setVisit:nil];
        }
        [self enterHome];
    }];
}

- (void)enterHome
{
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
    SLNavigationViewController *vc = [sb instantiateViewControllerWithIdentifier:@"SLNavigationViewController"];
    [self presentViewController:vc animated:YES completion:^{
        [UIView animateWithDuration:0.7 animations:^{
            self.imageView.alpha = 0.0;
            self.imageView.transform = CGAffineTransformMakeScale(1.5, 1.5);
        } completion:^(BOOL finished) {
            [self.imageView removeFromSuperview];
            self.imageView = nil;
        }];
    }];

}
-(UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}
@end
